Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.sesame;
16:
17: import cz.cvut.kbss.ontodriver.Closeable;
18: import cz.cvut.kbss.ontodriver.Connection;
19: import cz.cvut.kbss.ontodriver.OntologyStorageProperties;
20: import cz.cvut.kbss.ontodriver.config.ConfigParam;
21: import cz.cvut.kbss.ontodriver.config.Configuration;
22: import cz.cvut.kbss.ontodriver.config.ConfigurationParameter;
23: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
24: import cz.cvut.kbss.ontodriver.sesame.config.SesameConfigParam;
25: import cz.cvut.kbss.ontodriver.sesame.connector.ConnectorFactory;
26: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
27:
28: import java.util.*;
29:
30: class SesameDriver implements Closeable, ConnectionListener {
31:
32: private final List<ConfigurationParameter> CONFIGS = Arrays
33: .asList(ConfigParam.AUTO_COMMIT, ConfigParam.ONTOLOGY_LANGUAGE,
34: SesameConfigParam.USE_INFERENCE, SesameConfigParam.USE_VOLATILE_STORAGE);
35:
36: private final OntologyStorageProperties storageProperties;
37: private final Configuration configuration = new Configuration();
38: private boolean open;
39: private final ConnectorFactory connectorFactory;
40:
41: private final Set<SesameConnection> openedConnections;
42:
43: SesameDriver(OntologyStorageProperties storageProperties, Map<String, String> properties) {
44: assert storageProperties != null;
45: assert properties != null;
46:
47: this.storageProperties = storageProperties;
48: configuration.addConfiguration(properties, CONFIGS);
49: this.openedConnections = new HashSet<>();
50: this.connectorFactory = ConnectorFactory.getInstance();
51: this.open = true;
52: }
53:
54: @Override
55: public void close() throws OntoDriverException {
56: if (!open) {
57: return;
58: }
59: try {
60: for (SesameConnection c : openedConnections) {
61: c.removeListener(this);
62: c.close();
63: }
64: connectorFactory.close();
65: } catch (Exception e) {
66: if (e instanceof OntoDriverException) {
67: throw (OntoDriverException) e;
68: } else {
69: throw new SesameDriverException(e);
70: }
71: } finally {
72: this.open = false;
73: }
74: }
75:
76: @Override
77: public boolean isOpen() {
78: return open;
79: }
80:
81: Connection acquireConnection() throws SesameDriverException {
82: assert open;
83: final SesameAdapter adapter = new SesameAdapter(connectorFactory.createStorageConnector(
84: storageProperties, configuration), configuration);
85: final SesameConnection c = new SesameConnection(adapter);
86: c.setLists(new SesameLists(adapter, c::ensureOpen, c::commitIfAuto));
87: c.setTypes(new SesameTypes(adapter, c::ensureOpen, c::commitIfAuto));
88: c.setProperties(new SesameProperties(adapter, c::ensureOpen, c::commitIfAuto));
89: openedConnections.add(c);
90: c.addListener(this);
91: return c;
92: }
93:
94: @Override
95: public void connectionClosed(Connection connection) {
96: if (connection == null) {
97: return;
98: }
99: openedConnections.remove(connection);
100: }
101: }